package com.example.service.impl;


import com.example.entity.ExpRequestNumber;
import com.example.mapper.ExpRequestNumberMapper;
import com.example.service.ExpRequestNumberService;
import com.example.utils.BasicUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("expRequestNumberService")
public class ExpRequestNumberServiceImpl implements ExpRequestNumberService {
    private Logger logger = LoggerFactory.getLogger(ExpRequestNumberServiceImpl.class);
    @Autowired
    private ExpRequestNumberMapper expRequestNumberMapper;

    @Override
    public ExpRequestNumber selectNumberSingleByObject(String segment, String param1, String param2, String param3) {
        ExpRequestNumber dto = new ExpRequestNumber();
        dto.setSegment(segment); //号码段
        dto.setParam1(param1); //参数1
        dto.setParam2(param2);//参数2
        dto.setParam3(param3);//参数3
        return this.expRequestNumberMapper.selectByExample(dto);
    }

    @Override
    public void updateNumberById(Long index, Long infoId) {
        ExpRequestNumber dto = new ExpRequestNumber();
        dto.setInfoId(infoId); //主键
        dto.setCurrentIndex(index); //当前值

        this.expRequestNumberMapper.updateByPrimaryKey(dto);
    }

    @Override
    public void deleteByPrimaryKey(Long infoId) {
        this.expRequestNumberMapper.deleteByPrimaryKey(infoId);
    }

    @Override
    public String createRequestNumber(String segment, String param1, String param2, String param3) {
        ExpRequestNumber dto = this.selectNumberSingleByObject(segment, param1, param2, param3);
        if (dto != null) {
            System.out.println();
            Integer format = dto.getFormat();
            String prefix = dto.getPrefix();
            String rule = BasicUtils.createNumber(prefix, format);
            String num = (prefix != null ? prefix : "") + String.format(rule, dto.getCurrentIndex() + 1);
            this.updateNumberById(dto.getCurrentIndex() + 1, dto.getInfoId());
            return num;
        }
        return "";
    }
}
